package com.zbkj.front.controller;

import cn.hutool.core.util.ObjectUtil;
import com.zbkj.common.exception.ExceptionCodeEnum;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.UserGiftCardReceiveRequest;
import com.zbkj.common.response.*;
import com.zbkj.service.service.StoreGiftCardUserService;
import com.zbkj.service.service.StoreGiftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Swallows
 * @date 2022-08-22 15:54
 */
@RestController
@RequestMapping("api/front/giftCard")
@Api(tags = "个人 -- 礼品卡管理")
@RequiredArgsConstructor
public class UserGiftCardController {

    private final StoreGiftCardUserService giftCardUserService;
    private final StoreGiftService storeGiftService;

    @ApiOperation(value = "我的礼品卡")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="type", value="类型，usable-可用，unusable-不可用", required = true),
            @ApiImplicitParam(name="page", value="页码", required = true),
            @ApiImplicitParam(name="limit", value="每页数量", required = true)
    })
    public CommonResult<CommonPage<StoreGiftCardUserResponse>> getList(@RequestParam(value = "type") String type,
                                                                       @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(giftCardUserService.getMyGiftCardList(type, pageParamRequest));
    }

    @ApiOperation(value = "领卡")
    @RequestMapping(value = "/receive", method = RequestMethod.POST)
    public CommonResult<String> receive(@RequestBody UserGiftCardReceiveRequest request) {
        if (giftCardUserService.receiveGiftCard(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("使用礼品卡")
    @PostMapping("/use")
    public CommonResult<StoreGiftCardInfoResponse> exchange(@RequestBody UserGiftCardReceiveRequest request) {

        return CommonResult.success(giftCardUserService.useCard(request));
    }

    @ApiOperation("储值型礼品卡核销")
    @GetMapping("/storeValue/verification")
    public CommonResult<String> verification() {

        if (giftCardUserService.verification()) {
            return CommonResult.success("核销成功");
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("获取礼品卡可兑换商品列表")
    @GetMapping("/gift/list/{cardId}")
    public CommonResult<CommonPage<StoreGiftH5Response>> enableList(@PathVariable("cardId") Integer cardId, @ModelAttribute PageParamRequest pageParamRequest) {

        return CommonResult.success(CommonPage.restPage(storeGiftService.getEnableListByCardId(cardId, pageParamRequest)));
    }

    @ApiOperation("获取赠送礼品卡的key")
    @GetMapping("/key/{id}")
    public CommonResult<String> getShareKey(@PathVariable("id") Integer id) {

        String key = giftCardUserService.getShareKey(id);
        if (ObjectUtil.isNotNull(key)) {
            return CommonResult.success(key, ExceptionCodeEnum.SUCCESS.getMessage());
        } else {
            return CommonResult.failed(ExceptionCodeEnum.FAILED.getMessage());
        }
    }

    @ApiOperation("立即领取")
    @GetMapping("/draw")
    public CommonResult<String> draw(@RequestParam(value = "key") String key) throws Exception {

        if (giftCardUserService.draw(key)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("获取礼品详情")
    @GetMapping("/detail/{id}")
    public CommonResult<StoreGiftDetailResponse> info(@PathVariable("id") Integer id) {

        return CommonResult.success(storeGiftService.getDetailH5(id));
    }
}
